# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql


class SecondDemoPipeline:
    
    fp = None
    # 重写父类，该方法只会在爬虫开始时进行一次
    def open_spider(self,spider):
        print('开始爬虫')
        self.fp = open('./baidu.txt','w',encoding='utf-8')

    # 有几个item对象，就会调用几次
    def process_item(self, item, spider):
        title = item['title']
        now_time = item['now_time']
        self.fp.write('爬取时间：'+now_time+"---"+'新闻标题：'+title+'\n')
        return item #该item会传递给下一个管道类
    
    # 重写父类，该方法只会在爬虫结束时进行一次
    def close_spider(self,spider):
        print('结束爬虫')
        self.fp.close()



class MysqlPipeline:
    conn = None
    cur = None
    # 重写父类，该方法只会在爬虫开始时进行一次
    def open_spider(self,spider):
        print('mysql开始爬虫')
        self.conn = pymysql.Connect(host="124.220.0.182",user='root',password='123456',database='scrapy',port=3306,charset='utf8')
        

    # 有几个item对象，就会调用几次
    def process_item(self, item, spider):
        title = item['title']
        now_time = item['now_time']
        

        self.cur = self.conn.cursor()
        try:
            self.cur.execute("insert into baidu (now_time,title) values(%s,%s)",(now_time,title))
            self.conn.commit()
        except Exception as e:
            print(e)
            self.conn.rollback()
        return item #该item会传递给下一个管道类
    
    # 重写父类，该方法只会在爬虫结束时进行一次
    def close_spider(self,spider):
        print('mysql结束爬虫')
        self.conn.close()
        